<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="试用期">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="员工" prop="nickName">
        <el-input
            v-model="queryParams.nickName"
            placeholder="请输入员工名称"
            clearable
          />
        <!-- <el-select v-model="queryParams.userId" placeholder="员工" clearable>
          <el-option v-for="dict in userList" :key="dict.value" :label="dict.nickName"
            :value="dict.userDocBaseId" />
        </el-select> -->
      </el-form-item>
      <!-- <el-form-item label="审批状态" prop="status"> -->
      <!-- <el-select
          v-model="queryParams.status"
          placeholder="审批状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="合同到期">
        <el-date-picker v-model="dateRange2" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="状态" prop="statusValue">
        <el-select v-model="queryParams.statusValue" placeholder="状态" clearable>
          <el-option v-for="dict in dict.type.sys_contract_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['personnel:contract:add']"
          >新增</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['personnel:contract:edit']">修改</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['personnel:contract:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['personnel:contract:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="120" />
      <el-table-column label="员工" prop="nickName" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="合同类型" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_contract_type" :value="scope.row.contractTypeValue" />
        </template>
      </el-table-column>
      <el-table-column label="签约时间" prop="signData" :show-overflow-tooltip="true" />
      <!-- <el-table-column
        label="试用结束日期"
        prop="tryEndDate"
        :show-overflow-tooltip="true"
      /> -->
      <!-- <el-table-column label="合同状态" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_contract_status"
            :value="scope.row.statusValue"
          />
        </template>
      </el-table-column> -->
      <el-table-column label="失效时间" prop="endDate" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" width="240" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            @click="handleZzsq(scope.row)"
            v-hasPermi="['system:role:edit']"
            >转正申请</el-button
          > -->
          <el-button size="mini" type="text" icon="el-icon-view" v-hasPermi="['company:certificate:info']"
            @click="handleDetail(scope.row)" v-if="scope.row.laborContractId != null">详情</el-button>

          <el-button size="mini" type="text" icon="el-icon-edit" v-if="scope.row.laborContractId == null"
            @click="handleAdd(scope.row)" v-hasPermi="['personnel:contract:add']">完善</el-button>
          <el-button v-else size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['company:certificate:edit']">修改</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['company:certificate:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>
<script>
import { listContract, delContract } from "@/api/finance/personnel/contract.js";
import { checkRow } from "@/utils/ruoyi.js";
import {
  listDocBase,
} from "@/api/finance/personnel/file.js";
export default {
  name: "Contract",
  dicts: [
    "sys_contract_item",
    "sys_contract_type",
    "sys_contract_property",
    "sys_contract_status",
    "sys_contract_time",
    "sys_contract_zzbs",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      dataList: [],
      // 弹出层标题
      // 日期范围
      dateRange: [],
      dateRange2: [],
      // 数据范围选项
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        statusValue: undefined,
        userId: undefined,
        person: 0,
        nickName:''
      },
      userList:[]
    };
  },
  created() {
    const str = sessionStorage.getItem('queryParams');
    if (str) {
      this.queryParams = JSON.parse(str);
      if (this.queryParams.startEndDate) {
        this.dateRange2.push(this.queryParams.startEndDate, this.queryParams.endEndDate)
      }
      if (this.queryParams.startTryDate) {
        this.dateRange.push(this.queryParams.startTryDate, this.queryParams.endTryDate)
      }
      setTimeout(() => {
        sessionStorage.removeItem('queryParams');
      }, 500)
    }
    this.getList();
    this.listDocBase()
  },
  methods: {
    getList() {
      this.loading = true;
      this.queryParams = this.addDateRangeTime(
        this.queryParams,
        this.dateRange,
        "startTryDate",
        "endTryDate"
      );
      listContract(
        this.addDateRangeTime(
          this.queryParams,
          this.dateRange2,
          "startEndDate",
          "endEndDate"
        )
      ).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
     /** 查询用户列表 */
     listDocBase() {
      listDocBase({pageNum:1,pageSize:1000,person: 0}).then((response) => {
        this.userList = response.rows;
      });
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.typeValue = data.value;
      this.handleQuery();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleDetail(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      this.$router.push("./addContract?isInfo=true&id=" + row.laborContractId);
    },
    handleZzsq(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      this.$router.push("./zzsq?userId=" + row.userId);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.dateRange2 = [];

      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.laborContractId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd(e) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      console.log(e);
      if(e.isSure == 0){
        this.$confirm('当前人员暂未确认档案信息，无法完善合同信息?', '提示', {
          confirmButtonText: '确定',
          // cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          
        })
        return
      }
        
      this.$router.push("./addContract?userDocBaseId="+e.userDocBaseId + "&nickName=" + e.nickName);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      const configId = row.laborContractId || this.ids;
      this.$router.push("./addContract?isEdit=true&id=" + configId);
    },
    handleExport() {
      this.download(
        "system/contract/export",
        {
          ...this.queryParams,
        },
        `劳动合同记录表_${this.parseTime(
          new Date().getTime(),
          "{y}-{m}-{d}"
        )}.xlsx`
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.laborContractId || this.ids;
      let name = checkRow(
        userIds,
        this.dataList,
        "laborContractId",
        "DELETE",
        "nickName"
      );

      this.$modal
        .confirm('是否确认删除姓名为"' + name + '"的数据项？')
        .then(function () {
          return delContract(userIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>
